package dataStructure.LinkedList;

/**
 * @author lhn
 * @date 2024-04-18 21:22
 */

public class Josephu {
    public static void main(String[] args) {
        CircleSingleLinkedList list = new CircleSingleLinkedList();
        list.addBoy(5);
        list.showBoy();

        System.out.println("This is countBoy");
        list.countBoy(1,2,5);   //2->4->1->5->3
    }
}
//创建一个环形的单向链表
class CircleSingleLinkedList {
    //创建一个first节点,当前没有编号
    private Boy first = new Boy(-1);
    //添加小孩节点
    public void addBoy(int nums) {
        // nums数据校验
        if (nums < 1) {
            System.out.println("nums的值不正确");
            return;
        }
        Boy curBoy = null;  //辅助指针
        //使用for循环创建环形链表
        for (int i = 1; i <= nums ; i++) {
            //根据编号创建节点
            Boy boy = new Boy(i); //将要添加的boy
            //如果是第一个小孩
            if (i == 1) {
                first = boy;
                first.setNext(first);
                curBoy = first;
            } else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }
    //遍历
    public void showBoy() {
         //判断链表是否为空
        if (first == null) {
            System.out.println("链表为空");
            return;
        }
        //辅助指针
        Boy curBoy = first;
        while(true) {
            System.out.printf("小孩的编号 %d \n", curBoy.getNo());
            if (curBoy.getNext() == first) {
                //遍历完成
                break;
            }
            curBoy = curBoy.getNext();  //后移
        }
    }
    //根据用户的输入，计算出圈顺序
    /*
    *
    * @author lihaonan
    * @date 2024/4/18 22:25
     * @param startNo   第几个开始(先移动startNo-1次)
     * @param countNum  数几下
     * @param nums  表示最初有多少人
    */
    public void  countBoy(int startNo, int countNum, int nums){
        if (first == null || startNo < 1 || startNo > nums) {
            System.out.println("参数输入有误，请重新输入");
            return;
        }
        Boy helper = first;
        //创建辅助指针helper，指向环形链表的最后一个节点
        while (true) {
            if (helper.getNext() == first) {
                //当前helper已经指向最后一个小孩了
                break;
            }
            helper = helper.getNext();
        }
        //小孩报数前，先让first和helper移动k-1次
        for (int i = 0; i < startNo - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        //当小孩报数时，让first和helper指针同时的移动 m - 1次，然后出圈
        //循环直到圈中只有一个节点
        while (true) {
            if (helper == first) {
                break;
            }
            //让first和helper指针同时的移动 m - 1次，然后出圈
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            //现在first指向的节点就是要出圈的小孩节点
            System.out.printf("小孩%d出圈\n",first.getNo());
            first = first.getNext();    //后移
            helper.setNext(first);
        }
        System.out.printf("小孩%d出圈\n",first.getNo());
    }
}
//表示一个节点
class Boy {
    private int no; //编号
    private Boy next;   //指向下一个节点,默认null

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }

}